package com.plumejob.executor;

import com.plumejob.executor.util.HttpsClient;
import com.plumejob.executor.util.TaskUtils;
import org.quartz.*;

/**
 * @author : pdl
 * @date : 2021/2/5 17:24
 */
public class PlumeJob implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String name = jobExecutionContext.getJobDetail().getKey().getName();
        PlumeJobDetail jobDetail = PlumeJobContext.getJobDetailByName(name);
        //是当前节点执行
        if (PlumeJobContext.isMaster == 1 && jobDetail.getActive() == 1) {
            jobDetail.setScheduler(jobExecutionContext.getScheduler());
            jobDetail.setCronTrigger(jobExecutionContext.getTrigger());
            if (jobDetail.getNextFireTime() != null) {
                jobDetail.setLastFireTime(jobDetail.getNextFireTime());
            }
            boolean isSuccess = TaskUtils.invokMethod(jobDetail);
            //TODO 执行日志回填server
            pushExecuteLog(jobDetail);
        }
    }

    private void pushExecuteLog(PlumeJobDetail jd) {
        try {
            HttpsClient.post(PlumeJobContext.host + "/plumejob/log", jd);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
